package edu.nf.takeoutdemo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.nf.takeoutdemo.commons.constant.StatusConstant;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.mapper.CategoryMapper;
import edu.nf.takeoutdemo.mapper.DishMapper;
import edu.nf.takeoutdemo.mapper.SetmealMapper;
import edu.nf.takeoutdemo.model.converter.CategoryConverter;
import edu.nf.takeoutdemo.model.dto.CategoryDTO;
import edu.nf.takeoutdemo.model.dto.CategoryPageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Category;
import edu.nf.takeoutdemo.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/25
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final DishMapper dishMapper;
    private final CategoryMapper categoryMapper;
    private final SetmealMapper setmealMapper;
    private final CategoryConverter  categoryConverter;

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = categoryConverter.toCategory(categoryDTO);
        category.setStatus(StatusConstant.DISABLE);
        //设置创建时间、修改时间、创建人、修改人
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.add(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPageNum(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteById(Long id) {
        Integer count = dishMapper.countByCategoryId(id);
        if(count>0){
            throw new RuntimeException("当前分类下有菜品，不能删除");
        }
        count =  setmealMapper.countByCategoryId(id);
        if(count>0){
            throw new RuntimeException("当前分类下有套餐，不能删除");
        }
        categoryMapper.delete(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = categoryConverter.toCategory(categoryDTO);
        //设置修改时间、修改人
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

    @Override
    public List<Category> categoryAllList() {
        return categoryMapper.categoryAllList();
    }
}
